using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class CT_HoaDonDAO
    {
        public void ThemCTHoaDon(int MaHD, int MaP, Double DonGia, DateTime NgayThanhToan, DateTime NgayBD)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();

                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_ThemCTHoaDon";

                cmd.Parameters.Add("@MaHD", SqlDbType.Int);
                cmd.Parameters.Add("@MaP", SqlDbType.Int);
                cmd.Parameters.Add("@DonGia", SqlDbType.Money);
                cmd.Parameters.Add("@NgayThanhToan", SqlDbType.DateTime);
                cmd.Parameters.Add("@NgayThue", SqlDbType.DateTime);

                cmd.Parameters["@MaHD"].Value = MaHD;
                cmd.Parameters["@MaP"].Value = MaP;
                cmd.Parameters["@DonGia"].Value = DonGia;
                cmd.Parameters["@NgayThanhToan"].Value = NgayThanhToan;
                cmd.Parameters["@NgayThue"].Value = NgayBD;

                cmd.ExecuteNonQuery();

                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }

        public Double LayTongTienTheoMaHD(int MaHD)
        {
            Double TongTien = 0;
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                DataTable dt = new DataTable();
                con = dp.ConnectionData();
                con.Open();

                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_LayTongTienTheoMaHD";

                cmd.Parameters.Add("@MaHD", SqlDbType.Int);
                cmd.Parameters["@MaHD"].Value = MaHD;

                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                TongTien = Convert.ToDouble(dt.Rows[0]["TongTien"].ToString());

                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return TongTien;
        }
    }
}
